package patrol.mobile.com.mobilepatrol.test;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.orhanobut.logger.Logger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmResults;
import patrol.mobile.com.mobilepatrol.PatrolApp;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;

import static patrol.mobile.com.mobilepatrol.PatrolApp.realm;

/**
 * Created by apple on 17/10/14.
 */

public class AddActivity1 extends Activity {
    private JSONArray teachers = new JSONArray();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add1);
//        initData();
        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        initData();
                        realm.createAllFromJson(Teacher.class, teachers);
                    }
                });


            }
        });
        findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<Teacher> teachers = realm.copyFromRealm(realm.where(Teacher.class).findAll());
                Logger.d(GsonUtil.GsonString(teachers));
            }
        });
        findViewById(R.id.addTea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PatrolApp.realm.beginTransaction();
                Teacher tea = new Teacher();
                tea.name = "teacher incert";
                PatrolApp.realm.insert(tea);
                PatrolApp.realm.commitTransaction();
            }
        });
        findViewById(R.id.addTea2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PatrolApp.realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        Teacher tea = realm.createObject(Teacher.class);
                        tea.name = "addTea2";
                    }
                });


            }
        });
        findViewById(R.id.addStu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PatrolApp.realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {


                        RealmResults<Teacher> results = PatrolApp.realm.where(Teacher.class).equalTo("id", "1").findAll();
                        if (results.size() > 0) {
                            Student s = PatrolApp.realm.createObject(Student.class, "3");
                            s.name = "teacher1 stu3";
                            results.get(0).students.add(s);
                        }
                    }
                });


            }
        });
        findViewById(R.id.addStu2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Student stu = new Student();
                stu.name = "addStu3";
                stu.id = "3";
                PatrolApp.realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        realm.copyToRealmOrUpdate(stu);
                    }
                });


            }
        });
    }

    private void initData() {
        teachers = new JSONArray();
        //两个老师
        //每个老师的学生集合
        JSONArray stus = new JSONArray();
        //每个老师
        JSONObject teacher = new JSONObject();
        try {
            teacher.put("id", 1 + "");
            teacher.put("name", "teacher" + 1);
            for (int j = 0; j < 3; j++) {
                JSONObject stu = new JSONObject();
                stu.put("id", j+"");
                stu.put("name", "teacher" + 1 + "stu" + j);
                stus.put(stu);
            }
            teacher.put("students", stus);
            teachers.put(teacher);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        Logger.d(teachers);
    }
}
